/**   
 * @Title: ViewerUtil.java 
 * @Package com.esun.util 
 * @Description: (视图工具类,存放通用视图类工具) 
 * @author City Jiangsy@500wan.com   
 * @date 2011-5-31 下午03:32:15  
 * copy right 深圳市易讯天空网络技术有限公司
 */
package com.esun.util;

import android.app.Activity;
import android.app.Dialog;
import android.app.ProgressDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.KeyEvent;

import android.view.View;
import android.view.View.OnClickListener;
import android.view.WindowManager;

import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.EditText;

import android.widget.ImageButton;
import android.widget.TextView;
import android.widget.Toast;
import com.esun.config.Config;
import com.esun.model.UserInfo;
import com.esun.rabbit2.viewer.MainAct;
import com.esun.rabbit2.viewer.mainact.lotteryhallact.daletouagp.DaletouChooseAct;
import com.esun.rabbit2.viewer.mainact.lotteryhallact.shuangseqiuagp.ShuangseqiuChooseAct;
import com.esun.rabbit2.viewer.mainact.mylotteryact.recharg.RechargeMenuPage;
import com.esun.ui.R;
import com.esun.util.custom.GridViewNoScroll;

/**
 * @ClassName: ViewerUtil
 * @Description: (视图工具类,存放通用视图类工具)
 * @author City Jiangsy@500wan.com
 * @date 2011-5-31 下午03:32:15
 * 
 */
public class ViewerUtil {
	// 标志位,标识目前窗口是否有显示的dialog
	private static boolean IS_DIALOG_SHOW = false;
	private static int _count = 0;
	private static Dialog randomDialog;
	public static boolean isShowRegist = false;

	/**
	 * @Title: showTipDialog
	 * @Description: (公用提示对话框,仅包含一个确认按键操作,注意传入)
	 * @param @param context 当前显示的activity
	 * @param @param strMsg 需要显示的信息
	 * @param @param isActEnd 为true时关闭当前Activity
	 * @return void 返回类型
	 * @throws
	 */
	public static int getRandomCount() {
		return _count;
	}

	public static void showTipDialog(final Context context, String strMsg, final boolean isActEnd) {
		final Dialog popTipDialog = new Dialog(context, R.style.dialog);
		popTipDialog.getWindow().setContentView(R.layout.viewerutil_dialog_showtipdialog);
		if (!IS_DIALOG_SHOW && !((Activity) context).isFinishing()) {
			popTipDialog.show();
			IS_DIALOG_SHOW = true;
			TextView tvMsg = (TextView) popTipDialog.getWindow().findViewById(R.id.viewerutildialogshowtipdialog_tv_msg);
			tvMsg.setText(strMsg);

			Button btnOk = (Button) popTipDialog.getWindow().findViewById(R.id.viewerutildialogshowtipdialog_btn_ok);
			btnOk.setOnClickListener(new View.OnClickListener() {

				@Override
				public void onClick(View v) {

					popTipDialog.dismiss();
					IS_DIALOG_SHOW = false;
				}
			});

			popTipDialog.setOnDismissListener(new DialogInterface.OnDismissListener() {

				@Override
				public void onDismiss(DialogInterface dialog) {

					IS_DIALOG_SHOW = false;
					if (isActEnd) {
						((Activity) context).finish();
					}
				}
			});
		}
	}

	public static Dialog showBuySucceed(final Context context, String strMsg, OnClickListener listenerToHall, OnClickListener listenerBack) {
		Dialog popBuySucceedDialog = null;
		popBuySucceedDialog = new Dialog(context, R.style.dialog);
		popBuySucceedDialog.getWindow().setContentView(R.layout.viewerutil_dialog_showbuysucceed);
		// if (!IS_DIALOG_SHOW) {
		// popBuySucceedDialog.show();
		// IS_DIALOG_SHOW = true;
		TextView tvMsg = (TextView) popBuySucceedDialog.getWindow().findViewById(R.id.viewerutildialogshowbuysucceed_tv_msg);
		tvMsg.setText(strMsg);

		Button btnToHall = (Button) popBuySucceedDialog.getWindow().findViewById(R.id.viewerutildialogshowbuysucceed_btn_toHall);
		btnToHall.setOnClickListener(listenerToHall);

		Button btnBack = (Button) popBuySucceedDialog.getWindow().findViewById(R.id.viewerutildialogshowbuysucceed_btn_back);
		btnBack.setOnClickListener(listenerBack);
		popBuySucceedDialog.setOnDismissListener(new DialogInterface.OnDismissListener() {

			@Override
			public void onDismiss(DialogInterface dialog) {

				// IS_DIALOG_SHOW = false;
				((Activity) context).finish();

			}
		});
		// }
		return popBuySucceedDialog;
	}

	public static boolean isInChooseView;

	public static void showRandomDialog(final Context context, String strMsg, final int min, final int max, final int scale, int randomNum, OnClickListener listener) {
		randomDialog = new Dialog(context, R.style.dialog);
		randomDialog.getWindow().setContentView(R.layout.viewerutil_dialog_showrandomdialog);
		randomDialog.show();
		TextView tvMsg = (TextView) randomDialog.getWindow().findViewById(R.id.viewerutildialogshowrandomdialog_tv_msg);
		tvMsg.setText(strMsg);
		final EditText edtCount = (EditText) randomDialog.getWindow().findViewById(R.id.viewerutildialogshowrandomdialog_edt_count);
		if (randomNum == 0) {
			edtCount.setText(Integer.toString(min));
			_count = min;
		} else {
			edtCount.setText(Integer.toString(randomNum));
			_count = randomNum;
		}

		edtCount.addTextChangedListener(new TextWatcher() {

			@Override
			public void onTextChanged(CharSequence s, int start, int before, int count) {

				if (!(String.valueOf(s).equals(Config.NULLMARK)) && s != null) {
					_count = Integer.parseInt(String.valueOf(s));
					if (_count > max || _count < min) {
						Toast.makeText(context, "请输入" + Integer.toString(min) + Config.HORIZONTAL_LINE + Integer.toString(max) + "之间的数字", Toast.LENGTH_SHORT).show();
						edtCount.setText(Integer.toString(min));
						_count = min;
					} else {

					}
				} else {
					_count = min;
				}
			}

			@Override
			public void beforeTextChanged(CharSequence s, int start, int count, int after) {

			}

			@Override
			public void afterTextChanged(Editable s) {

			}
		});
		edtCount.setOnFocusChangeListener(new View.OnFocusChangeListener() {

			@Override
			public void onFocusChange(View v, boolean hasFocus) {

				if (!hasFocus && _count == min) {
					edtCount.setText(Config.NUM1);
				}

			}
		});
		ImageButton btnAdd = (ImageButton) randomDialog.getWindow().findViewById(R.id.viewerutildialogshowrandomdialog_btn_add);
		ImageButton btnReduce = (ImageButton) randomDialog.getWindow().findViewById(R.id.viewerutildialogshowrandomdialog_btn_reduce);

		btnAdd.setOnClickListener(new View.OnClickListener() {

			@Override
			public void onClick(View v) {

				if (_count < max) {
					_count++;
					edtCount.setText(Integer.toString(_count));
				} else {
					_count = max;
					edtCount.setText(Integer.toString(_count));
				}
			}
		});
		btnReduce.setOnClickListener(new View.OnClickListener() {

			@Override
			public void onClick(View v) {

				if (_count > min) {
					_count--;
					edtCount.setText(Integer.toString(_count));
				} else {
					_count = min;
					edtCount.setText(Integer.toString(_count));
				}
			}
		});
		Button btnSubmit = (Button) randomDialog.getWindow().findViewById(R.id.viewerutildialogshowrandomdialog_btn_ok);
		btnSubmit.setOnClickListener(listener);
		Button btnCancel = (Button) randomDialog.getWindow().findViewById(R.id.viewerutildialogshowrandomdialog_btn_cancle);
		btnCancel.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {

				ShuangseqiuChooseAct.isChooseView = true;
				DaletouChooseAct.isChooseView = true;
				randomDialog.dismiss();
			}
		});
		randomDialog.setOnDismissListener(new DialogInterface.OnDismissListener() {

			@Override
			public void onDismiss(DialogInterface dialog) {

				if (isInChooseView) {
					ShuangseqiuChooseAct.isChooseView = false;
					DaletouChooseAct.isChooseView = false;
					isInChooseView = false;
				} else {
					ShuangseqiuChooseAct.isChooseView = true;
					DaletouChooseAct.isChooseView = true;
				}
			}
		});
	}

	public static void dismissRandomDialog() {
		randomDialog.dismiss();
	}

	public static ProgressDialog showProgressDialog(Context context, String msg) {

		ProgressDialog progress = null;
		progress = new ProgressDialog(context);
		// progress.getWindow().setContentView(R.layout.progress_dialog);
		progress.setMessage(msg);
		// TextView tvMsg = (TextView)progress.getWindow().findViewById(R.id.progress_dialog_msg);
		// tvMsg.setText(msg);

		progress.setOnKeyListener(new DialogInterface.OnKeyListener() {

			@Override
			public boolean onKey(DialogInterface dialog, int keyCode, KeyEvent event) {

				return true;

			}
		});

		return progress;
	}

	public static void showRegistSucceedDialog(final Context context) {
		final Dialog registSucceedDialog = new Dialog(context, R.style.dialog);
		registSucceedDialog.getWindow().setContentView(R.layout.viewerutil_dialog_registsucceed);
		OnClickListener listener = new OnClickListener() {

			@Override
			public void onClick(View v) {

				switch (v.getId()) {
				case R.id.viewerutildialogregistsucceed_btn_cancel:
					registSucceedDialog.dismiss();
					((MainAct) ((Activity) context).getParent()).setChildActivity(MainAct.LOTTERY_HALL_ACTIVITY);
					break;

				case R.id.viewerutildialogregistsucceed_btn_recharge:
					registSucceedDialog.dismiss();
					Intent intent = new Intent();
					intent.setClass((Activity) context, RechargeMenuPage.class);
					context.startActivity(intent);
					break;
				default:
					break;
				}
			}
		};
		Button btnCancel = (Button) registSucceedDialog.getWindow().findViewById(R.id.viewerutildialogregistsucceed_btn_cancel);
		btnCancel.setOnClickListener(listener);
		btnCancel.setText("购彩大厅");
		Button btnRecharge = (Button) registSucceedDialog.getWindow().findViewById(R.id.viewerutildialogregistsucceed_btn_recharge);
		btnRecharge.setOnClickListener(listener);
		isShowRegist = true;
		registSucceedDialog.show();
	}

	public static void showZeroMoneyDialog(final Context context) {
		final Dialog zeroMoneyDialog = new Dialog(context, R.style.dialog);
		zeroMoneyDialog.getWindow().setContentView(R.layout.viewerutil_dialog_zeromoney);
		OnClickListener listener = new OnClickListener() {

			@Override
			public void onClick(View v) {

				switch (v.getId()) {
				case R.id.viewerutildialogzeromoney_btn_cancel:
					zeroMoneyDialog.dismiss();
					break;

				case R.id.viewerutildialogzeromoney_btn_recharge:
					zeroMoneyDialog.dismiss();
					Intent intent = new Intent();
					intent.setClass((Activity) context, RechargeMenuPage.class);
					context.startActivity(intent);
					UserInfo.getUserInfo().setBuyingRecharge(true);
					break;
				default:
					break;
				}
			}
		};
		Button btnCancel = (Button) zeroMoneyDialog.getWindow().findViewById(R.id.viewerutildialogzeromoney_btn_cancel);
		btnCancel.setOnClickListener(listener);
		Button btnRecharge = (Button) zeroMoneyDialog.getWindow().findViewById(R.id.viewerutildialogzeromoney_btn_recharge);
		btnRecharge.setOnClickListener(listener);
		((TextView) zeroMoneyDialog.getWindow().findViewById(R.id.viewerutildialogzeromoney_tv_userName)).setText(UserInfo.getUserInfo().getUserName() + "您好！您的余额：0.00元");
		zeroMoneyDialog.show();
	}

	public static void showBuyingRechargeDialog(final Context context, String cardNum) {
		final Dialog buyingRechargeDialog = new Dialog(context, R.style.dialog);
		buyingRechargeDialog.getWindow().setContentView(R.layout.viewerutil_dialog_buyingrecharge);
		OnClickListener listener = new OnClickListener() {

			@Override
			public void onClick(View v) {

				switch (v.getId()) {
				case R.id.viewerutildialogbuyingrecharge_btn_goon:
					buyingRechargeDialog.dismiss();
					((Activity) context).finish();
					UserInfo.getUserInfo().setBuyingRecharge(false);
					break;

				case R.id.viewerutildialogbuyingrecharge_btn_toHall:
					buyingRechargeDialog.dismiss();
					((Activity) context).finish();
					break;
				default:
					break;
				}
			}
		};
		Button btnGoon = (Button) buyingRechargeDialog.getWindow().findViewById(R.id.viewerutildialogbuyingrecharge_btn_goon);
		btnGoon.setOnClickListener(listener);
		Button btnToHall = (Button) buyingRechargeDialog.getWindow().findViewById(R.id.viewerutildialogbuyingrecharge_btn_toHall);
		btnToHall.setOnClickListener(listener);
		((TextView) buyingRechargeDialog.getWindow().findViewById(R.id.viewerutildialogbuyingrecharge_tv_msg)).setText("您的银行卡充值订单提交成功!您将使用绑定的银行卡:" + cardNum
				+ "进行支付,请留意银联02096585的来电。(如您第一次使用建行充值,为了保证您的交易安全,银联将会审核您的资料,来电可能延迟3分钟,请耐心等候)");
		buyingRechargeDialog.setOnDismissListener(new Dialog.OnDismissListener() {

			@Override
			public void onDismiss(DialogInterface dialog) {

				((Activity) context).finish();
			}
		});

		buyingRechargeDialog.show();
	}

	/**
	 * @Title: showRandomNumDialog
	 * @Description: 显示随机选号个数菜单Dialog
	 * @param: @param context
	 * @param: @param dialogAdapter
	 * @param: @return 设定文件
	 * @return: Dialog 返回类型
	 * 
	 */
	public static Dialog showRandomNumDialog(final Context context, BaseAdapter dialogAdapter) {
		Dialog randomNumDialog = new Dialog(context, R.style.dialog);
		randomNumDialog.getWindow().setContentView(R.layout.viewerutil_dialog_randomnum);
		randomNumDialog.setCanceledOnTouchOutside(true);
		randomNumDialog.getWindow().clearFlags(WindowManager.LayoutParams.FLAG_DIM_BEHIND);
		GridViewNoScroll gridView = (GridViewNoScroll) randomNumDialog.getWindow().findViewById(R.id.GridView_randomnum);
		gridView.setAdapter(dialogAdapter);
		return randomNumDialog;
	}

}
